package com.openweixin.sheknows.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.openweixin.sheknows.common.Pager;
import com.openweixin.sheknows.domain.Expert;
import com.openweixin.sheknows.domain.Question;
import com.openweixin.sheknows.service.ExpertService;
import com.openweixin.sheknows.service.QuestionService;
import com.openweixin.sheknows.util.SessionUtil;
@Controller
@RequestMapping("/expert")
public class ExpertController extends BaseController{

	@Autowired
	private ExpertService service;
	public static final String LOCK_QUESTION_KEY="lock_question";
	
	@Autowired
	private QuestionService questionService;

	@RequestMapping
	public String toMain(){
		return "Expert";
	}

	
	@RequestMapping("/questionListView")
	public String listView(Pager<Question> pager){
		pager.setSort("datetime");
		pager.setLimit(5);
		pager.addNumberParam("status",0);
		pager = questionService.findQuestionPager(pager);
		return "expert/questionListView";
	}
	
	@RequestMapping("/details")
	public ModelAndView questionDetails( Integer id){
		Integer uid=SessionUtil.getUserID();
		boolean isLock=questionService.lockQuestionByExtpert(id,uid);
		if(isLock){
			SessionUtil.getSession().setAttribute(LOCK_QUESTION_KEY,id);
		}
		
		Question question=questionService.get(id);
		return new ModelAndView("expert/details","question",question).addObject("isLock", isLock);
	}
	@RequestMapping("/add")
	@ResponseBody 
	public String add(@RequestBody Expert bean){
		service.save(bean);
		return SUCCESS; 
	}

	@RequestMapping("/update")
	@ResponseBody 
	public String update(@RequestBody Expert bean){
		service.update(bean);
		return SUCCESS; 
	}

	@RequestMapping("/delete")
	@ResponseBody 
	public String delete(Integer[] ids){
		service.delete(ids);
		return SUCCESS; 
	}

}
